package com.masterclient.app.modules.timelimited.adapter;

import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eoner.baselibrary.bean.firstpager.RecommendPagerBean;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.eoner.baselibrary.utils.Utils;
import com.masterclient.app.widget.AutoLineUtil;


public class TimeLimitedBuyAdapter extends BaseQuickAdapter<RecommendPagerBean.ShProduct, BaseViewHolder> {

    private String status;

    public TimeLimitedBuyAdapter(String status) {
        super(R.layout.home_time_product_item);
        this.status = status;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    protected void convert(BaseViewHolder helper, RecommendPagerBean.ShProduct shProduct) {

        TextView tvOfficialPrice = helper.getView(R.id.tv_official_price);
        AutoLineUtil viewTags = helper.getView(R.id.al_tag);
        TextView desc = helper.getView(R.id.desc);
        TextView price = helper.getView(R.id.price);
        TextView buy_btn = helper.getView(R.id.buy_btn);
        TextView sale_progress = helper.getView(R.id.sale_progress);
        ProgressBar progressBar = helper.getView(R.id.progressBar);
        ImageView image = helper.getView(R.id.image);
        ImageView ivActivity = helper.getView(R.id.iv_activity);
        desc.setText(shProduct.getSh_name());
        desc.setMaxLines(2);
        desc.setEllipsize(TextUtils.TruncateAt.END);

        //tags
        if (shProduct.getSh_product_tags() != null && shProduct.getSh_product_tags().size()>0){
            viewTags.setVisibility(View.VISIBLE);
            viewTags.removeAllViews();
            for (int i=0;i < shProduct.getSh_product_tags().size(); i++){
                View view = LayoutInflater.from(mContext).inflate(R.layout.item_new_goods_details_tag,null,false);
                TextView tvTag =view.findViewById(R.id.tv_tag);
                tvTag.setText(shProduct.getSh_product_tags().get(i));
                viewTags.addView(view);
            }
        }else {
            viewTags.setVisibility(View.GONE);
        }

        //官方价
        if (shProduct.getPriceTagBean() != null && !TextUtils.isEmpty(shProduct.getPriceTagBean().getMarket_price_tag())){
            if (!TextUtils.isEmpty(shProduct.getSh_market_price())){
                tvOfficialPrice.setText("¥" +shProduct.getSh_market_price());
                tvOfficialPrice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
                tvOfficialPrice.setVisibility(View.VISIBLE);
            }
        }else {
            tvOfficialPrice.setVisibility(View.GONE);
        }

        //viewHolder.price.setText("￥" + shProduct.getSh_show_price());
        Utils.set_price_style(price, "¥ " + shProduct.getSh_show_price());
        //Utils.set_price_style(line_price,"¥" + shProduct.getSh_line_price());

        Utils.setDin(price, mContext);

        sale_progress.setText("已售"+shProduct.getSh_display() + "%");
        int progress = (int) Float.valueOf(shProduct.getSh_display()).floatValue();
        progressBar.setProgress(progress);
        GlideUtil.displayImage(mContext,shProduct.getSh_image(),image,R.mipmap.defaults_1);
//        App.picasso.load(shProduct.getSh_image())
//                //.resize(ScreenUtils.dp2px(180),ScreenUtils.dp2px(120))
//                .placeholder(R.mipmap.defaults_1)
//                .into(image);

        if (TextUtils.isEmpty(shProduct.getSh_partner_commission()) ||
                Double.parseDouble(shProduct.getSh_partner_commission()) == 0) {
        } else {
        }

        if ("4".equals(status) | "5".equals(status)) {
            progressBar.setVisibility(View.GONE);
            sale_progress.setVisibility(View.GONE);
        } else {
            progressBar.setVisibility(View.VISIBLE);
            sale_progress.setVisibility(View.VISIBLE);
        }

        if (Integer.valueOf(shProduct.getSh_stock()) > 0) {
            helper.getView(R.id.sell_out_layout).setVisibility(View.INVISIBLE);
        } else {
            helper.getView(R.id.sell_out_layout).setVisibility(View.VISIBLE);
            sale_progress.setText("已售罄");
        }

        helper.addOnClickListener(R.id.buy_btn);
        if ("1".equals(shProduct.getSh_remind_state())) {
            //1: 未开抢（开抢提醒)
            buy_btn.setVisibility(View.VISIBLE);
            buy_btn.setText("开售提醒");
            buy_btn.setBackgroundColor(Color.parseColor("#212121"));
            buy_btn.setTextColor(Color.parseColor("#FFFFFF"));
        } else if ("2".equals(shProduct.getSh_remind_state())) {
            //2: 取消提醒
            buy_btn.setVisibility(View.VISIBLE);
            buy_btn.setText("已设置提醒");
            buy_btn.setBackgroundResource(R.drawable.time_limited_have_remain_bkg);
            buy_btn.setTextColor(Color.parseColor("#333333"));
        } else {
            //其他
            if (Integer.valueOf(shProduct.getSh_stock()) <= 0 | "4".equals(status) | "5".equals(status)) {
                buy_btn.setVisibility(View.INVISIBLE);
            } else {
                buy_btn.setVisibility(View.VISIBLE);
            }

            if ("2".equals(status)) {
                if (Integer.parseInt(shProduct.getSh_stock()) > 0){
                    buy_btn.setText("活动已结束");
                }else {
                    buy_btn.setText("已售罄");
                }
                buy_btn.setBackgroundResource(R.drawable.time_limited_end0_bkg);
            } else {
                if (Integer.parseInt(shProduct.getSh_stock()) > 0){
                    buy_btn.setVisibility(View.VISIBLE);
                    buy_btn.setText("立即抢购");
                    buy_btn.setBackgroundColor(Color.parseColor("#212121"));
                }else {
                    buy_btn.setText("已售罄");
                    buy_btn.setBackgroundResource(R.drawable.time_limited_end0_bkg);
                }
            }
            buy_btn.setTextColor(Color.parseColor("#FFFFFF"));
        }

        double ratio = 1;
        if (!TextUtils.isEmpty(shProduct.getSh_image_width())){
            ratio = ScreenUtils.dp2px(107)/Double.parseDouble(shProduct.getSh_image_width());
        }
        if(shProduct.getSh_icon_info()!=null
                && !TextUtils.isEmpty(shProduct.getSh_icon_info().shIconHeight)
                &&!TextUtils.isEmpty(shProduct.getSh_icon_info().shIconWidth)) {
            int leftMargin = (int) (Integer.parseInt(shProduct.getSh_icon_info().shIconLeftMargin)*ratio);
            int topMargin = (int) (Integer.parseInt(shProduct.getSh_icon_info().shIconTopMargin)*ratio);
            int bottomMargin = (int) (Integer.parseInt(shProduct.getSh_icon_info().shIconBottomMargin)*ratio);
            ViewTransformUtil.glideImageView(mContext,shProduct.getSh_icon_info().shIconImage,ivActivity,R.mipmap.defaults);
            ivActivity.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivActivity.getLayoutParams();
            params.height = (int) (Integer.valueOf(shProduct.getSh_icon_info().shIconHeight)*ratio);
            params.width = (int) (Integer.valueOf(shProduct.getSh_icon_info().shIconWidth)*ratio);
            params.addRule(RelativeLayout.ALIGN_BOTTOM,0);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT,0);
            params.setMargins(0, 0, 0, 0);
            switch (shProduct.getSh_icon_info().shIconMarginType) {
                case "1":
                    params.addRule(RelativeLayout.ALIGN_BOTTOM,R.id.image);
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    params.setMargins(leftMargin, topMargin, 0, bottomMargin);
                    break;
                case "2":
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    params.setMargins(leftMargin, topMargin, 0, bottomMargin);
                    break;
            }
            ivActivity.setLayoutParams(params);
        }else {
            ivActivity.setVisibility(View.GONE);
        }

    }

}
